package com.ermace.robo.jumper;

import com.ermace.robo.jumper.game.Sound;

import android.annotation.TargetApi;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.ServiceConnection;
import android.content.SharedPreferences;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.IBinder;
import android.util.DisplayMetrics;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.animation.AnimationUtils;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivity extends Activity {

	private static double SCREEN_WIDTH;
	private static double SCREEN_HEIGHT;

	public static double getScreenWidth() {
		return SCREEN_WIDTH;
	}

	public static double getScreenHeight() {
		return SCREEN_HEIGHT;
	}

	private Intent difficultyIntent;
	private Intent highScoresIntent;
	private Intent instructionIntent;
	private ImageButton sndBtn;
	private ImageButton mscBtn;
	private static SharedPreferences parameters;
	private Drawable soundOn;
	private Drawable soundOff;
	private Drawable musicOn;
	private Drawable musicOff;
	private Intent music;
	private boolean musicStarted = false;

	private ImageButton playButton;
	private Button highScoreButton;
	private Button instBtn;

	public static boolean startingNewActivity = false;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		//Remove title bar
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		//Remove notification bar
		this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);

		setContentView(R.layout.menu);
		difficultyIntent = new Intent(this, DifficultyActivity.class);
		highScoresIntent = new Intent(this, HighScores.class);	
		instructionIntent = new Intent(this, InstructionActivity.class);

		parameters = getSharedPreferences("parameters", MODE_PRIVATE);

		playButton = (ImageButton) findViewById(R.id.playButton);

		highScoreButton = (Button) findViewById(R.id.highScoreButton);

		sndBtn = (ImageButton) findViewById(R.id.sndButton);
		sndBtn.setOnClickListener(sndBtnListener);

		DisplayMetrics metrics = new DisplayMetrics();
		getWindowManager().getDefaultDisplay().getMetrics(metrics);

		SCREEN_HEIGHT = metrics.heightPixels;
		SCREEN_WIDTH = metrics.widthPixels;

		soundOn = getResources().getDrawable(R.drawable.sound);        
		soundOff= getResources().getDrawable(R.drawable.sound_disabled);
		musicOn = getResources().getDrawable(R.drawable.music);        
		musicOff= getResources().getDrawable(R.drawable.music_disabled);

		if(!parameters.getBoolean("sound", true)) {
			sndBtn.setImageDrawable(soundOff);
			Sound.setVolume(0);
		}

		ImageView img = (ImageView) findViewById(R.id.titleImg);
		img.startAnimation(AnimationUtils.loadAnimation(this, R.anim.move_down));

		mscBtn = (ImageButton) findViewById(R.id.mscButton);
		mscBtn.setOnClickListener(mscBtnListener);

		instBtn = (Button) findViewById(R.id.instBtn);

		music = new Intent();
		music.setClass(this,MusicService.class);
		if(!parameters.getBoolean("music", true))
		{
			mscBtn.setImageDrawable(musicOff);
		}
		else{
			startService(music);
			musicStarted = true;
		}
		doBindService();

		//if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
		playButton.setOnTouchListener(new OnTouchListener() {			

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				//if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
				playButton.setImageResource(R.drawable.play_button_pressed);

				switch(event.getAction()) {
				//					case MotionEvent.ACTION_HOVER_EXIT:
				//						playButton.setImageResource(R.drawable.play_button);
				//						break;

				case MotionEvent.ACTION_OUTSIDE:
					playButton.setImageResource(R.drawable.play_button);
					break;

				case MotionEvent.ACTION_UP:
					playButton.setImageResource(R.drawable.play_button);
					startingNewActivity = true;
					startActivity(difficultyIntent);
					return true;
				}

				return true;
				//				} else
				//					return checkButtons(event);
			}
		});

		instBtn.setOnTouchListener(new OnTouchListener() {			

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				//				if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
				instBtn.setBackgroundResource(R.drawable.instruction_button_pressed);

				switch(event.getAction()) {
				//					case MotionEvent.ACTION_HOVER_EXIT:
				//						instBtn.setBackgroundResource(R.drawable.instruction_button);
				//						break;

				case MotionEvent.ACTION_OUTSIDE:
					instBtn.setBackgroundResource(R.drawable.instruction_button);
					break;

				case MotionEvent.ACTION_UP:
					instBtn.setBackgroundResource(R.drawable.instruction_button);
					startingNewActivity = true;
					startActivity(instructionIntent);
					return true;
				}

				return true;
				//				} else
				//					return checkButtons(event);
			}
		});


		highScoreButton.setOnTouchListener(new OnTouchListener() {			

			@Override
			public boolean onTouch(View v, MotionEvent event) {
				//				if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
				highScoreButton.setBackgroundResource(R.drawable.highscore_button_pressed);

				switch(event.getAction()) {
				//					case MotionEvent.ACTION_HOVER_EXIT:
				//						highScoreButton.setBackgroundResource(R.drawable.highscore_button);
				//						break;

				case MotionEvent.ACTION_OUTSIDE:
					highScoreButton.setBackgroundResource(R.drawable.highscore_button);
					break;

				case MotionEvent.ACTION_UP:
					highScoreButton.setBackgroundResource(R.drawable.highscore_button);
					startingNewActivity = true;
					startActivity(highScoresIntent);
					return true;
				}

				return true;
				//				} else
				//					return checkButtons(event);
			}
		});
		/*} else {
			LinearLayout view = (LinearLayout) findViewById(R.id.mainMenu);		//TODO fixe
			view.setOnTouchListener(new OnTouchListener() {

				public boolean onTouch(View v, MotionEvent event) {
					return checkPlayButton(event);
				}

			});
		}*/
	}
	/**
	 * @param event
	 * @param btn
	 * @return
	 */
	/*@TargetApi(11)
	private boolean checkButtons(MotionEvent event) {

		int action = event.getActionMasked();

	    if(action == MotionEvent.ACTION_DOWN || action == MotionEvent.ACTION_MOVE) {
	    	checkPlayButton(event);
	    	checkInstructionButton(event);
	    } 
	    else if (action == MotionEvent.ACTION_UP) {
	    	playButton.setImageResource(R.drawable.play_button);
	    	instBtn.setBackgroundResource(R.drawable.instruction_button);

			if (playIsPressed)
				startActivity(difficultyIntent);
			else if (instBtnIsPressed)
				startActivity(instructionIntent);

			playIsPressed = false;
			instBtnIsPressed = false;
			return true;
	    } 
	    else if (action == MotionEvent.ACTION_HOVER_EXIT) {
	    	playIsPressed = false;
			instBtnIsPressed = false;
	    }

	    if (playIsPressed)
	    	playButton.setImageResource(R.drawable.play_button_pressed);
	    else
	    	playButton.setImageResource(R.drawable.play_button);

	    if (instBtnIsPressed)
	    	instBtn.setBackgroundResource(R.drawable.instruction_button_pressed);
	    else
	    	instBtn.setBackgroundResource(R.drawable.instruction_button);

	    return false;
	}

	/**
	 * @param event
	 */
	@TargetApi(11)
	/*private void checkPlayButton(MotionEvent event) {
		double btnX = playButton.getX() + 50;
		double btnY = playButton.getY();
//	double radius = Math.min(btn.getHeight() / 2, btn.getWidth() / 2);
		double radius = playButton.getTop() + btnY;

		double x = event.getX();
		double y = event.getY();

		double dist = Math.sqrt(Math.pow(btnX - x, 2) + Math.pow(btnY - y, 2));
		if (dist > radius)
			playIsPressed = false;
		else {
			playIsPressed = true;
			instBtnIsPressed = false;
		}
	}*/

	/**
	 * @param event
	 */
	/*@TargetApi(11)
	private void checkInstructionButton(MotionEvent event) {
		double btnX = instBtn.getX() + 50;
		double btnY = instBtn.getY();
		double width = instBtn.getWidth();
		double height = instBtn.getHeight();

		double x = event.getX();
		double y = event.getY();

		if ( x > (btnX + width / 2) || x < (btnX - width / 2)
				|| y > (btnY + height / 2) || y < (btnY - height / 2) )
			instBtnIsPressed = false;
		else {
			instBtnIsPressed = true;
			playIsPressed = false;
		}
	} */



	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		//getMenuInflater().inflate(R.menu.activity_main, menu);
		return true;
	}
	@Override
	public void onDestroy()
	{
		mServ.pauseMusic();
		super.onDestroy();
		doUnbindService();


	}

	public OnClickListener sndBtnListener = new OnClickListener()
	{

		@Override
		public void onClick(View v) {
			SharedPreferences.Editor editor = parameters.edit();
			if(!parameters.getBoolean("sound", true))
			{
				sndBtn.setImageDrawable(soundOn);
				editor.putBoolean("sound", true);
				editor.commit();
				Sound.setVolume(1);
			}
			else{
				sndBtn.setImageDrawable(soundOff);
				editor.putBoolean("sound", false);
				editor.commit();
				Sound.setVolume(0);
			}
		}

	};

	public OnClickListener mscBtnListener = new OnClickListener()
	{

		@Override
		public void onClick(View v) {
			SharedPreferences.Editor editor = parameters.edit();
			if(!parameters.getBoolean("music", true))
			{
				mscBtn.setImageDrawable(musicOn);
				editor.putBoolean("music", true);
				editor.commit();
				if(!musicStarted)
					startService(music);
				else
					mServ.resumeMusic();
			}
			else{
				mscBtn.setImageDrawable(musicOff);
				editor.putBoolean("music", false);
				editor.commit();
				mServ.pauseMusic();
			}
		}

	};

	private boolean mIsBound = false;
	private static MusicService mServ;
	private ServiceConnection Scon =new ServiceConnection(){

		@Override
		public void onServiceConnected(ComponentName name, IBinder binder) {
			MusicService.ServiceBinder sBinder = (MusicService.ServiceBinder) binder;
			mServ = sBinder.getService();
		}

		@Override
		public void onServiceDisconnected(ComponentName name) {
			mServ = null;
		}
	};

	void doBindService(){
		bindService(music,
				Scon,Context.BIND_AUTO_CREATE);
		mIsBound = true;
	}

	void doUnbindService()
	{
		if(mIsBound)
		{
			unbindService(Scon);
			mIsBound = false;
		}
	}

	public static void pauseMusic()
	{
		if(mServ != null && !startingNewActivity)
			mServ.pauseMusic();
		startingNewActivity = false;
	}

	public static void resumeMusic()
	{
		if(mServ != null && !mServ.isPlaying() && parameters.getBoolean("music", true))
			mServ.resumeMusic();
	}


	@Override
	protected void onResume() {
		super.onResume();
			resumeMusic();
	}
	
	@Override
	protected void onPause() {
		super.onPause();
			pauseMusic();
	}
	
	

}
